# coding:utf8
import os
import sys
import time

LOG_FILE = 'slow_query_3306.log'


def quick_sort(lists, left, right):
    # 快速排序
    if left >= right:
        return lists
    key = lists[left]
    low = left
    high = right
    while left < right:
        while left < right and lists[right] >= key:
            right -= 1
        lists[left] = lists[right]
        while left < right and lists[left] <= key:
            left += 1
        lists[right] = lists[left]
    lists[right] = key
    quick_sort(lists, low, left - 1)
    quick_sort(lists, left + 1, high)
    return lists


def main():
    file = open(LOG_FILE)
    arr = file.readlines()
    file.close()
    
    counter = 0
    dic_result = {}  ## key=2018-07-12T12:00:00 value=[1,2,3]
    arr_arr = []
    for line in arr:
        if line.find('scalper_search') >= 0:
            query_cost_time = int(round(float(arr[counter + 1].split()[2])))
            query_time = arr[counter - 1].split()[2][0:16]
            if query_cost_time >= 5:
                arr_arr.append(query_cost_time)

            # if dic_result.get(query_time, None) == None:
            #     dic_result[query_time] = []
            # dic_result[query_time].append(query_cost_time)

        counter = counter + 1

    dic_data = {}
    for key in dic_result:
        group(key, dic_result[key], dic_data)

    print quick_sort(arr_arr, 0, len(arr_arr) - 1)


def group(key, arr, dic_data):
    dic = {}
    for item in arr:
        if dic.get(item, None) == None:
            dic[item] = 1
            continue
            dic[item] = dic[item] + 1

    dic_data[key] = dic


if __name__ == '__main__':
    main()
